<?php

/**
 * Created by 观察者模式 - 观察中介类.
 * User: 楚羽幽
 * Date: 2018/2/8
 * Time: 0:28
 * QQ：1113581489
 */
namespace app\common\logic\Observer;
class Observer implements \SplSubject
{
    //观察者列表
    public $observers = null;

    //构造函数
    public function __construct()
    {
        $this->observers = new \SplObjectStorage();
    }

    //加入观察者
    public function attach(\SplObserver $observer)
    {
        $this->observers->attach($observer);
    }

    //移除观察者
    public function detach(\SplObserver $observer)
    {
        $this->observers->detach($observer);
    }

    //通知观察者
    public function notify()
    {
        $this->observers->rewind();
        while ($this->observers->valid())
        {
            $observer = $this->observers->current();
            $observer->update($this);
            $this->observers->next();
        }
    }
}